import os
from pathlib import Path


class FileTool:
    # __file__ 始终指向当前脚本文件（即定义该方法的脚本文件）的路径，无论方法被谁调用。
    # os.path.abspath(__file__) 会将该路径转换为绝对路径，但不会因为调用方的位置而改变结果。
    # 获取项目根目录
    # project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    project_root = Path(__file__).parent.parent

    @classmethod
    def read_bash_file(cls, file_path):
        """
        读取bash文本文件内容
        """
        with open(file_path, "r") as file:
            content = file.read()
        return content

    @classmethod
    def get_shellscripts_dir(cls):
        target_path = cls.project_root / "shellscripts"
        if not os.path.exists(target_path):
            os.makedirs(target_path)
        return target_path

    @classmethod
    def get_file_size(cls, file_path):
        """
        获取文件大小，自动换算合适的大小
        :param file_path: 绝对路径
        :return: 文件大小
        """
        try:
            size_bytes = os.path.getsize(file_path)
            units = ["B", "KB", "MB", "GB", "TB"]
            for unit in units:
                if size_bytes < 1024:
                    return f"{round(size_bytes, 2)} {unit}"
                size_bytes /= 1024
            return f"{round(size_bytes, 2)} PB"
        except FileNotFoundError:
            return "文件不存在"
        except PermissionError:
            return "无权限访问"

    @classmethod
    def get_video_dir(cls):
        """
        获取上传视频的保存位置
        :return: upload_files的完整路径
        """
        target_path = cls.project_root / "video_files"
        # target_path = os.path.join(cls.project_root, "video_files")
        # 确保文件夹存在
        if not os.path.exists(target_path):
            os.makedirs(target_path)
        return target_path

    @classmethod
    def get_srt_dir(cls):
        """
        生成字幕的文件存放的位置
        :return: srt_files的完整路径
        """
        target_path = cls.project_root / "srt_files"
        # target_path = os.path.join(cls.project_root, "srt_files")
        # 确保文件夹存在
        if not os.path.exists(target_path):
            os.makedirs(target_path)
        return target_path

    @classmethod
    def get_translated_srt_dir(cls):
        """
        生成翻译后的字幕的文件存放的位置
        :return: translate_srt_files的完整路径
        """
        target_path = cls.project_root / "translated_srt_files"
        # target_path = os.path.join(cls.project_root, "translated_srt_files")
        # 确保文件夹存在
        if not os.path.exists(target_path):
            os.makedirs(target_path)
        return target_path

    @classmethod
    def get_models_dir(cls):
        """
        存放下载好的模型的位置
        """
        target_path = cls.project_root / "models"
        # target_path = os.path.join(cls.project_root, "models")
        # 确保文件夹存在
        if not os.path.exists(target_path):
            os.makedirs(target_path)
        return target_path

    @classmethod
    def find_video_files(cls, directory):
        """
        去掉了前面的路径，只有文件名
        """
        video_extensions = {'.mp4', '.avi', '.mov', '.mkv', '.flv', '.wmv', '.webm'}
        path = Path(directory)
        return [file.name for file in path.rglob('*') if file.suffix.lower() in video_extensions]

    @classmethod
    def get_lianji_dir(cls):
        target_path = cls.project_root / "lianji"
        if not os.path.exists(target_path):
            os.makedirs(target_path)
        return target_path
